import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/handle/xindong_user_handle.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/models/xindong_money_class.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/dialog/xindong_picker_sheet.dart';
import 'package:xindong/widgets/xindong_common_select_item.dart';
import 'package:xindong/widgets/xindong_common_switch_item.dart';

class XinDongSettingMoneyYeMian extends StatefulWidget {
  const XinDongSettingMoneyYeMian({Key? key}) : super(key: key);

  @override
  State<XinDongSettingMoneyYeMian> createState() => _XinDongSettingMoneyYeMianState();
}

class _XinDongSettingMoneyYeMianState extends State<XinDongSettingMoneyYeMian> {
  final _userHandle = XinDongUserHandle();
  List<XinDongMoneyClass> chatList = [];
  List<XinDongMoneyClass> voiceList = [];
  List<XinDongMoneyClass> videoList = [];

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      // 拉取用户信息
      XinDongUserController.to.requestConfig();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false, //输入框抵住键盘 内容不随键盘滚动
      appBar: XinDongAppBar("收费设置"),
      body: GetBuilder<XinDongUserController>(builder: (controller) {
        var chatPrice = "免费";
        if (controller.userInfo?.configPersonal?.chat_price != "0.00") {
          chatPrice = "${controller.userInfo?.configPersonal?.chat_price}金币/条";
        }
        var voicePrice = "免费";
        if (controller.userInfo?.configPersonal?.voice_price != "0.00") {
          voicePrice = "${controller.userInfo?.configPersonal?.voice_price}金币/分钟";
        }
        var videoPrice = "免费";
        if (controller.userInfo?.configPersonal?.video_price != "0.00") {
          videoPrice = "${controller.userInfo?.configPersonal?.video_price}金币/分钟";
        }
        return ListView(
          children: [
            XinDongCommonSelectItem(
              title: "消息价格",
              value: chatPrice,
              onTap: () async {
                if (chatList.isEmpty) {
                  final result = await _userHandle.userConfigEditList(type: 1);
                  if (result.isNotEmpty) {
                    chatList = result;
                  }
                }
                if (chatList.isNotEmpty) {
                  final dataList = chatList.map((e) => "${e.price}金币").toList();
                  XinDongPickerSheet.show(
                    title: "请选择价格",
                    selectText: chatPrice,
                    dataList: dataList,
                    doneCallback: (index, text) async {
                      final value = chatList[index].price;
                      final result = await _userHandle.userConfigEdit(key: "chat_price", value: value);
                      if (result) {
                        controller.userInfo?.configPersonal?.chat_price = value;
                        controller.update();
                      }
                    },
                  );
                }
              },
            ),
            XinDongCommonSwitchItem(
              title: "语音接听",
              value: controller.userInfo?.configPersonal?.voice_switch == 1,
              onChanged: (open) async {
                final value = open ? 1 : 0;
                final result = await _userHandle.userConfigEdit(key: "voice_switch", value: value);
                if (result) {
                  controller.userInfo?.configPersonal?.voice_switch = value;
                  controller.update();
                }
              },
            ),
            XinDongCommonSelectItem(
              title: "语音价格设置",
              value: voicePrice,
              onTap: () async {
                if (voiceList.isEmpty) {
                  final result = await _userHandle.userConfigEditList(type: 2);
                  if (result.isNotEmpty) {
                    voiceList = result;
                  }
                }
                if (voiceList.isNotEmpty) {
                  final dataList = voiceList.map((e) => "${e.price}金币").toList();
                  XinDongPickerSheet.show(
                    title: "请选择价格",
                    selectText: chatPrice,
                    dataList: dataList,
                    doneCallback: (index, text) async {
                      final value = voiceList[index].price;
                      final result = await _userHandle.userConfigEdit(key: "voice_price", value: value);
                      if (result) {
                        controller.userInfo?.configPersonal?.voice_price = value;
                        controller.update();
                      }
                    },
                  );
                }
              },
            ),
            XinDongCommonSwitchItem(
              title: "视频接听",
              value: controller.userInfo?.configPersonal?.video_switch == 1,
              onChanged: (open) async {
                final value = open ? 1 : 0;
                final result = await _userHandle.userConfigEdit(key: "video_switch", value: value);
                if (result) {
                  controller.userInfo?.configPersonal?.video_switch = value;
                  controller.update();
                }
              },
            ),
            XinDongCommonSelectItem(
              title: "视频价格设置",
              value: videoPrice,
              onTap: () async {
                if (videoList.isEmpty) {
                  final result = await _userHandle.userConfigEditList(type: 3);
                  if (result.isNotEmpty) {
                    videoList = result;
                  }
                }
                if (videoList.isNotEmpty) {
                  final dataList = videoList.map((e) => "${e.price}金币").toList();
                  XinDongPickerSheet.show(
                    title: "请选择价格",
                    selectText: chatPrice,
                    dataList: dataList,
                    doneCallback: (index, text) async {
                      final value = videoList[index].price;
                      final result = await _userHandle.userConfigEdit(key: "video_price", value: value);
                      if (result) {
                        controller.userInfo?.configPersonal?.video_price = value;
                        controller.update();
                      }
                    },
                  );
                }
              },
            ),
          ],
        );
      }),
    );
  }
}
